package cn.czh.org.service.impl;

import cn.czh.basic.service.impl.BaseServiceImpl;
import cn.czh.basic.util.PageList;
import cn.czh.org.domain.Department;
import cn.czh.org.mapper.DepartmentMapper;
import cn.czh.org.query.DepartmentQuery;
import cn.czh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    @Transactional//增删改的事务方式默认(readOnly = false,propagation = Propagation.REQUIRED)
    public void add(Department department) {
        departmentMapper.save(department);

        //如果没有选中上级部门就加上自己的id
        if (department.getParent_id()==null){
            department.setDirPath("/"+department.getId());
        }else { //如果选中了上级部门
            //根据父id查询它的dirPath
            String parentDirPath = departmentMapper.loadById(department.getParent_id()).getDirPath();
            //拼接父部门的dirPath与自己的id做拼接
            department.setDirPath(parentDirPath+"/"+department.getId());
        }
        departmentMapper.update(department);
    }



    @Override
    @Transactional//增删改的事务方式默认(readOnly = false,propagation = Propagation.REQUIRED)
    public void update(Department department) {
        departmentMapper.update(department);

        //如果没有选中上级部门就加上自己的id
        if (department.getParent_id()==null){
            department.setDirPath("/"+department.getId());
        }else { //如果选中了上级部门
            //根据父id查询它的dirPath
            String parentDirPath = departmentMapper.loadById(department.getParent_id()).getDirPath();
            //拼接父部门的dirPath与自己的id做拼接
            department.setDirPath(parentDirPath+"/"+department.getId());
        }
        departmentMapper.update(department);
    }



    @Override
    public List<Department> deptTree() {
        //1.查询所有数据
        List<Department> departments = departmentMapper.loadAll();

        //2.把查询到的数据放入map集合中
        Map<Long, Department> map = new HashMap<>();
        //3.遍历所有部门
        for (Department department : departments) {
            //4.将所有部门装到map中
            map.put(department.getId(),department);
        }
        //1.前端需要获得集合数据
        List<Department> deptTree = new ArrayList<>();
        //2.遍历数组
        for (Department department : departments) {
            //3.如果部门id为null就是顶级部门
            if (department.getParent_id() == null){
                deptTree.add(department);
            }else { //4.不是顶级部门
                //5.获取当前部门的上级部门id
                Long parent_id = department.getParent_id();
                //6.上级部门对象
                Department parentDepartment = map.get(parent_id);
                //7.将自己装入上级部门的集合中
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }


}
